package com.test.platform.service.developer;

import com.fasterxml.jackson.databind.JsonNode;
import com.test.platform.service.common.GitCodeApiClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class GitCodePRService {
    private static final Logger logger = LoggerFactory.getLogger(GitCodePRService.class);

    @Autowired
    private GitCodeApiClient gitCodeApiClient;
    
    @Autowired
    private DeveloperDataProcessor dataProcessor;

    @Value("${pr.update.enabled}")
    private boolean prUpdateEnabled;

    @Scheduled(fixedDelayString = "${gitcode.update.interval:21600000}")
    public void scheduledUpdatePRs() {
        if (!prUpdateEnabled) {
            logger.info("PR更新功能已禁用");
            return;
        }
        // 这里可以添加批量更新的逻辑
    }

    public void updateLatestPR(String owner, String name) {
        if (!prUpdateEnabled) {
            logger.info("PR更新功能已禁用，跳过更新: {}/{}", owner, name);
            return;
        }

        try {
            JsonNode prInfo = gitCodeApiClient.fetchLatestPR(owner, name);
            if (prInfo != null && prInfo.isArray() && !prInfo.isEmpty()) {
                dataProcessor.processPRInfo(owner, name, prInfo.get(0));
            } else {
                logger.warn("未获取到PR信息或格式不正确: {}/{}", owner, name);
            }
        } catch (Exception e) {
            logger.error("更新PR信息时发生错误: {}/{}", owner, name, e);
        }
    }
}